﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WWF.Client.UniversityApplicationService;
using System.ServiceModel;

namespace WWF.Client
{
    public partial class FrmSubimitApplication : Form
    {
        UniversityApplicationServiceClient proxy;

        public FrmSubimitApplication()
        {
            InitializeComponent();
        }

        private void cbxMedium_SelectedIndexChanged(object sender, EventArgs e)
        {
            gbxEnglishTest.Enabled = (cbxMedium.Text != "English");
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            pnlSubmit.Enabled = false;
            SubmitUniversityApplication();
        }

        private void SubmitUniversityApplication()
        {
            proxy = new UniversityApplicationServiceClient();
            proxy.BeginReceiveApplication(CreateApplicationRequest(), OnStartCompleted, null);
        }

        private ApplicationRequest CreateApplicationRequest()
        {
            return new ApplicationRequest
            {
                ApplicantName = tbxName.Text,
                ApplicantEmail = tbxEmail.Text,
                ApplicantID = tbxIDNo.Text,
                IDType = cbxIDType.Text,
                HighestDegree = cbxHighestDegree.Text,
                Program = tbxProgram.Text,
                ExperienceYears = Convert.ToDecimal(tbxExperience.Text),
                MediumOfInstructionIsEnglish = (cbxEnglishTest.Text== "English"),
                EngTestType = cbxEnglishTest.Text,
                EngTestScore = Convert.ToDecimal(tbxEnglishScore.Text)
            };
        }
        private void OnStartCompleted(IAsyncResult asr)
        {
            try
            {
                ApplicationResponse response = (ApplicationResponse)proxy.EndReceiveApplication(asr);
                DialogResult dialogResult;
                if (response.Status == "Duplicated")
                {
                    dialogResult = MessageBox.Show("Your application is duplicated and it is not successful. Please click Yes to do another submission. Please click No to exit the program.", "Application Submitted", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                }
                else
                {
                    dialogResult = MessageBox.Show("Your application has been successfully submitted. Please click Yes to do another submission. Please click No to exit the program.", "Application Submitted", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                }
                if (dialogResult == DialogResult.Yes)
                {
                    this.Invoke(
                    new MethodInvoker(() =>
                    {
                        pnlSubmit.Enabled = true;
                    }));
                }
                else if (dialogResult == DialogResult.No)
                {
                    this.Invoke(
                    new MethodInvoker(() =>
                    {
                        Application.Exit();
                    }));
                }
                proxy.Close();
            }
            catch (CommunicationException)
            {
                MessageBox.Show("There are some connection problems. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                proxy.Abort();
            }
            catch (TimeoutException)
            {
                MessageBox.Show("Application timeout problem. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                proxy.Abort();
            }
            catch (Exception)
            {
                proxy.Abort();
                throw;
            }
        }

    }
}
